﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Windows.ApplicationFramework;
using MyPublisher.Administration.Model;
using System.Collections.ObjectModel;

namespace MyPublisher.Administration.ViewModels
{
    interface IDocumentViewModel
    {
        Document Document { get; }
    }

    public class DocumentViewModel<T> : ViewModel<IView>, IDocumentViewModel
        where T : Document, new()
    {
        private readonly Document document;

        public DocumentViewModel(IView view,string name)
            : base(view)
        {
            this.document = new T() { Name = name, Modified = false };
            this.Items = new ObservableCollection<object>();
        }

        public Document Document { get { return document; } }

        private object selectedItem;
        public ObservableCollection<object> Items { get; private set; }
        public void InsertItems<T>(IEnumerable<T> items)
        {
            foreach (object item in items)
            {
                this.Items.Add(item);
            }
        }
        public void SelectFirst()
        {
            if (this.Items.Count > 0)
            {
                this.SelectedItem = this.Items[0];
            }
        }

        public object SelectedItem
        {
            get { return selectedItem; }
            set
            {
                if (selectedItem != value)
                {
                    selectedItem = value;
                    RaisePropertyChanged("SelectedItem");
                    if (SelectedView != null)
                    {
                        DocumentDetailViewModel detailModel = ((SelectedView as IView).DataContext as DocumentDetailViewModel);
                        if (detailModel != null)
                        {
                            detailModel.Item = value;
                        }
                    }
                }
            }
        }
    }

    public class DocumentDetailViewModel : ViewModel<IView>
    {
        private object item;
        public object Item
        {
            get { return item; }
            set
            {
                if (item != value)
                {
                    item = value;
                    RaisePropertyChanged("Item");
                }
            }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }
        public DocumentDetailViewModel(IView view, string name)
            : base(view)
        {
            this.Name = name;
        }
    }
}
